function varargout = FA_guiMain(varargin)
% FA_GUIMAIN MATLAB code for FA_GUIMAIN.fig
%      FA_GUIMAIN, by itself, creates a new FA_GUIMAIN or raises the existing
%      singleton*.
%
%      H = FA_GUIMAIN returns the handle to a new FA_GUIMAIN or the handle to
%      the existing singleton*.
%
%      FA_GUIMAIN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in FA_GUIMAIN.M with the given input arguments.
%
%      FA_GUIMAIN('Property','Value',...) creates a new FA_GUIMAIN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before FA_guiMain_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to FA_guiMain_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help FA_GUIMAIN

% Last Modified by GUIDE v2.5 15-Jan-2014 18:13:31

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @FA_guiMain_OpeningFcn, ...
                   'gui_OutputFcn',  @FA_guiMain_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end


% End initialization code - DO NOT EDIT


% --- Executes just before FA_GUIMAIN is made visible.
function FA_guiMain_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to FA_GUIMAIN (see VARARGIN)

% Choose default command line output for FA_GUIMAIN
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes FA_GUIMAIN wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = FA_guiMain_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in importExecutionTimesButton.
function importExecutionTimesButton_Callback(hObject, eventdata, handles)
% hObject    handle to importExecutionTimesButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global FileName;
global PathName;
FileName = '';
PathName = '';
[FileName,PathName] = uigetfile('*.mat','Select the MATLAB .mat file');
if length(FileName) > 1
set(handles.labelTasksFilename, 'String', FileName);
end;

% --- Executes during object creation, after setting all properties.
function numberOfFireflies_CreateFcn(hObject, eventdata, handles)
% hObject    handle to numberOfFireflies (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



% --- Executes during object creation, after setting all properties.
function gamma_CreateFcn(hObject, eventdata, handles)
% hObject    handle to gamma (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



% --- Executes during object creation, after setting all properties.
function betamin_CreateFcn(hObject, eventdata, handles)
% hObject    handle to betamin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



% --- Executes during object creation, after setting all properties.
function alpha_CreateFcn(hObject, eventdata, handles)
% hObject    handle to alpha (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function numberOfIterations_CreateFcn(hObject, eventdata, handles)
% hObject    handle to numberOfIterations (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in executeButton.
function executeButton_Callback(hObject, eventdata, handles)
% hObject    handle to executeButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global numOfFireflies;
global N_iteration;
global alpha;
global betamin;
global gamma;
global FileName;
global PathName;
numOfFireflies = str2num(get(handles.numberOfFireflies, 'String'));
N_iteration = str2num(get(handles.numberOfIterations, 'String'));
alpha = str2num(get(handles.alpha, 'String'));
betamin = str2num(get(handles.betamin, 'String'));
gamma = str2num(get(handles.gamma, 'String'));
mTab = [];
if length(FileName) > 1
mTab = importdata(strcat(PathName,FileName));
end;
if(size(mTab,1) > 0 && size(mTab,3) > 0)
Net_fa_ndim(mTab);
FA_guiResult;
end;


% --- Executes on button press in defineNetworkBtn.
function defineNetworkBtn_Callback(hObject, eventdata, handles)
% hObject    handle to defineNetworkBtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
FaGraphGui;

% --- Executes on button press in generateTasksBtn.
function generateTasksBtn_Callback(hObject, eventdata, handles)
% hObject    handle to generateTasksBtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global NetFileName;
global taskNumber;
global minExecTime;
global maxExecTime;
taskNumber = str2num(get(handles.taskNrLabel, 'String'));
minExecTime = str2num(get(handles.minTaskTimeLabel, 'String'));
maxExecTime = str2num(get(handles.maxTaskTimeLabel, 'String'));
global NetPathName;
global netDefinitionMatrix;
if length(NetFileName) > 1
    netDefinitionMatrix = importdata(strcat(NetPathName,NetFileName));
    mTab = Net_taskGeneration(netDefinitionMatrix, taskNumber,minExecTime,maxExecTime);
    str = inputdlg('Name of the MAT file','Save',1,{'mTab'});
    if ~isempty(str)
         eval(sprintf('save %s mTab',str{1}));
    end
end




% --- Executes during object creation, after setting all properties.
function taskNrLabel_CreateFcn(hObject, eventdata, handles)
% hObject    handle to taskNrLabel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes during object creation, after setting all properties.
function minTaskTimeLabel_CreateFcn(hObject, eventdata, handles)
% hObject    handle to minTaskTimeLabel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function maxTaskTimeLabel_CreateFcn(hObject, eventdata, handles)
% hObject    handle to maxTaskTimeLabel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in selectNetBtn.
function selectNetBtn_Callback(hObject, eventdata, handles)
% hObject    handle to selectNetBtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global NetFileName;
global NetPathName;
NetFileName = '';
NetPathName = '';
[NetFileName,NetPathName] = uigetfile('*.mat','Select the MATLAB .mat file');
if length(NetFileName) > 1
set(handles.labelNetName, 'String', NetFileName);
end;

% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

function numberOfFireflies_Callback(hObject, eventdata, handles)
% hObject    handle to numberOfFireflies (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of numberOfFireflies as text
%        str2double(get(hObject,'String')) returns contents of numberOfFireflies as a double

function gamma_Callback(hObject, eventdata, handles)
% hObject    handle to gamma (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of gamma as text
%        str2double(get(hObject,'String')) returns contents of gamma as a double

function betamin_Callback(hObject, eventdata, handles)
% hObject    handle to betamin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of betamin as text
%        str2double(get(hObject,'String')) returns contents of betamin as a double

function alpha_Callback(hObject, eventdata, handles)
% hObject    handle to alpha (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of alpha as text
%        str2double(get(hObject,'String')) returns contents of alpha as a double

function numberOfIterations_Callback(hObject, eventdata, handles)
% hObject    handle to numberOfIterations (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of numberOfIterations as text
%        str2double(get(hObject,'String')) returns contents of numberOfIterations as a double

% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


function taskNrLabel_Callback(hObject, eventdata, handles)
% hObject    handle to taskNrLabel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of taskNrLabel as text
%        str2double(get(hObject,'String')) returns contents of taskNrLabel as a double

function minTaskTimeLabel_Callback(hObject, eventdata, handles)
% hObject    handle to minTaskTimeLabel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of minTaskTimeLabel as text
%        str2double(get(hObject,'String')) returns contents of minTaskTimeLabel as a double

function maxTaskTimeLabel_Callback(hObject, eventdata, handles)
% hObject    handle to maxTaskTimeLabel (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of maxTaskTimeLabel as text
%        str2double(get(hObject,'String')) returns contents of maxTaskTimeLabel as a double
